const expressJoi = require("@escook/express-joi")
const express = require("express")
const router = express.Router()
const { getList, addStudent, putStudent, delStudent, fileAddStudents, fileGetStudents, getStudentById } = require("../controller/student")
const { add_schema, put_schema, del_schema, list_schema, getById_schema } = require("../utils/schema/student")
router.get("/list/:class_id", expressJoi(list_schema), getList)
router.get("/getById/:student_id", expressJoi(getById_schema), getStudentById)
router.post("/add", expressJoi(add_schema), addStudent)
router.put("/put", expressJoi(put_schema), putStudent)
router.delete("/delete/:id", expressJoi(del_schema), delStudent)


// 文件上传添加
// 导入解析 formdata 格式表单数据的包
const multer = require('multer')
// 导入处理路径的核心模块
const path = require('path')

const storage = multer.diskStorage({
  // destination:'public/uploads/'+new Date().getFullYear() + (new Date().getMonth()+1) + new Date().getDate(),
  destination(req, res, cb) {
    // + new Date().getFullYear() + (new Date().getMonth() + 1) + new Date().getDate()
    cb(null, path.join(__dirname, '../uploads/'))
  },
  filename(req, file, cb) {
    console.log(file);
    const filenameArr = file.originalname.split('.');
    cb(null, Date.now() + '.' + filenameArr[filenameArr.length - 1]);
  }
});

// // 创建 multer 的实例对象，通过 dest 属性指定文件的存放路径
const upload = multer({ storage });
router.post("/fileAdd", upload.single('students'), fileAddStudents)


// 获取列表文件
router.get("/fileGet/:class_id", expressJoi(list_schema), fileGetStudents)




module.exports = router